	set more off

use Data/Estimation_sample.dta, clear
	
	gen nexis_buildings  = ROOFME/PROOFME
		
		
	gen roofratio = ROOFME/ROOFTI
	gen roofratiobuildings = roofratio*nexis_buildings
		
	gen roofrelativeIV = (roofratio*nexis_buildings/[moduleprice])/1000

	
	merge 1:1 postcode contract_quarter using Data/sales_postcodequarter.dta
	
	keep if _merge ==3 
	drop _merge
	
	
	label variable Salesdriven "Sales Driven Acquisition"
	label variable Consumerdriven "Customer Driven Acquisition"
	label variable Renewal "Contract Renewal"
	
	
	
	xi: xtivreg2 green_p cuminstall  i.quarter   [aweight =NCustomer	] , fe cluster(postcode) 

	
	quietly summ green_plan [aweight =NCustomer	] if e(sample)
    local meandepvar=round(`r(mean)',.001)

	outreg2 using Analysis/FE_postcode_sales, bdec(3) label ctitle("FE")   ///
	keep(cum* ) addtext(Postcode FE, Yes, Year-Quarter FE, Yes, Mean dep var, 0`meandepvar') ///
	tex(fragment pretty) nonotes replace
 

 
	
	xi: xtivreg2 green_p (cuminstall = roofrelativeIV)  i.quarter   [aweight =NCustomer	] , fe cluster(postcode) 

	
	quietly summ green_plan [aweight =NCustomer	] if e(sample)
    local meandepvar=round(`r(mean)',.001)

	outreg2 using Analysis/FE_postcode_sales, bdec(3) label ctitle("IV") addstat(CDW F-test, e(widstat))  ///
	keep(cum* ) addtext(Postcode FE, Yes, Year-Quarter FE, Yes, Mean dep var, 0`meandepvar') ///
	tex(fragment pretty) nonotes append
 

 
	
	
	
	
	xi: xtivreg2 green_p cuminstall  i.quarter Salesdriven Consumerdriven [aweight =NCustomer	] , fe cluster(postcode) 

	
	quietly summ green_plan [aweight =NCustomer	] if e(sample)
    local meandepvar=round(`r(mean)',.001)

	outreg2 using Analysis/FE_postcode_sales, bdec(3) label ctitle("FE")   ///
	keep(cum* Sales Consumer) addtext(Postcode FE, Yes, Year-Quarter FE, Yes, Mean dep var, 0`meandepvar') ///
	tex(fragment pretty) nonotes append
 

 
 
	

 
 
 
	xi: xtivreg2 green_p (cuminstall = roofrela )  i.quarter Salesdriven Consumerdriven [aweight =NCustomer	], fe cluster(postcode) ffirst 

	eststo IVnonlinear1
	
	quietly summ green_plan  [aweight =NCustomer	] if e(sample)
    local meandepvar=round(`r(mean)',.001)

	outreg2 using Analysis/FE_postcode_sales, bdec(3) label ctitle("IV") addstat(CDW F-test, e(widstat))  ///
	keep(cum* Sales Consumer) addtext(Postcode FE, Yes, Year-Quarter FE, Yes, Mean dep var, 0`meandepvar') ///
	tex(fragment pretty) nonotes append
 
	
 
 
